//
//  ViewController.m
//  SignatrueForPdf
//
//  Created by glendale on 13-8-3.
//  Copyright (c) 2013年 glendale. All rights reserved.
//

#import "PdfViewController.h"
#import "SignViewController.h"

@interface PdfViewController ()

@end

@implementation PdfViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    //1判断是否有pdf
    
  
}
-(void)viewWillAppear:(BOOL)animated{
  
    NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"print.pdf"];
    NSURL *url = [NSURL fileURLWithPath:path];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.myWebView loadRequest:request];
    
    //
    [self addAllObserver];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    //
    [self removeAllObserver];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)signBtnSelect:(id)sender {
    
    SignViewController *Vcon = [[SignViewController alloc] initWithNibName:@"SignViewController" bundle:nil];
    [self presentViewController:Vcon animated:YES completion:nil];
}

- (IBAction)BackBtnselect:(UIButton *)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)printBtnSelect:(UIButton *)sender {
    
    NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"print.pdf"];
	NSData *data = [NSData dataWithContentsOfFile: path];
	
	UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
    
    pic.delegate = self;
    
    if  (pic && [UIPrintInteractionController canPrintData: data] ) {
        
		pic.delegate = self;
		
        UIPrintInfo *printInfo = [UIPrintInfo printInfo];
        printInfo.outputType = UIPrintInfoOutputGeneral;
        printInfo.jobName = [path lastPathComponent];
        printInfo.duplex = UIPrintInfoDuplexLongEdge;
        pic.printInfo = printInfo;
        pic.showsPageRange = YES;
        pic.printingItem = data;
		
        void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
            
			if (!completed && error)
				NSLog(@"FAILED! due to error in domain %@ with error code %u",error.domain, error.code);
        };
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
			[pic presentFromRect:sender.frame inView:self.view animated:YES completionHandler:completionHandler];
            
        } else {
			[pic presentAnimated:YES completionHandler:completionHandler];
		}
	}

}

- (void)printInteractionControllerDidDismissPrinterOptions:(UIPrintInteractionController *)printInteractionController
{
    NSLog(@"dismiss!!!");
}

//Tells the delegate that the print job is about to start.
- (void)printInteractionControllerWillStartJob:(UIPrintInteractionController *)printInteractionController
{
    NSLog(@"print!!!");
    
    MainMenuDataModel *modelIns = [MainMenuDataModel sharedMainMenuDataModel];
    
    NSMutableArray *paras=[NSMutableArray arrayWithObjects:@"jobSheetId",modelIns.jobSheetIdForPrint,@"printType", modelIns.printTypeForPrint, nil];
    [HelpClass getWebServiceDataByString:MM_PRINT_JOB_SHEET_ andArrObject:paras];
}

//注册消息
-(void)addAllObserver
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateJobSheetType:) name:MM_PRINT_JOB_SHEET_ object:nil];
}

//移除消息
-(void)removeAllObserver
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

//改变作业单状态
-(void)updateJobSheetType:(NSNotification*)notifcation
{
    NSDictionary *theJson = [NSJSONSerialization JSONObjectWithData:(NSData*)notifcation.object options:NSJSONReadingMutableLeaves error:Nil];
    NSLog(@"%@",theJson);
}

@end
